import { shell, screen, BrowserWindowConstructorOptions } from 'electron'
import { WindowCenter } from './win-center'
import { PLAYGROUND_FILE_URL, PRELOAD_FILE } from 'app/config'

export class MainWindow {
    static creat() {
        const screenWorkAreaSize = screen.getPrimaryDisplay().workAreaSize
        const option: BrowserWindowConstructorOptions = {
            width: screenWorkAreaSize.width,
            height: screenWorkAreaSize.height,
            minWidth: 960,
            minHeight: 640,
            // titleBarStyle: 'hidden',
            autoHideMenuBar: true,
            webPreferences: {
                nodeIntegration: true,
                webSecurity: false,
                preload: PRELOAD_FILE,
                enableRemoteModule: true,
                allowRunningInsecureContent: true,
            },
        }
        const win = WindowCenter.create('main', option)
        win.loadURL(PLAYGROUND_FILE_URL).then()
        win.webContents.on('will-navigate', (event, url) => {
            if (/^http(s)?:/.test(url)) {
                event.preventDefault()
                shell.openExternal(url).then()
            }
        })
        return win
    }

    static restore() {
        WindowCenter.restoreWindow('main')
    }

    static close() {
        WindowCenter.closeAllWindow()
    }
}
